package com.product.system.PdfSignHelloWorld;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.security.BouncyCastleDigest;
import com.itextpdf.text.pdf.security.DigestAlgorithms;
import com.itextpdf.text.pdf.security.MakeSignature;
import com.itextpdf.text.pdf.security.MakeSignature.CryptoStandard;
import com.itextpdf.text.pdf.security.PrivateKeySignature;
import com.lowagie.text.BadElementException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.Certificate;
public class C2_01_SignHelloWorld {
    public static final String KEYSTORE = "D:\\keystore\\beike.p12";
    public static final char[] PASSWORD = "111111".toCharArray();

    public void sign(String src, String dest,
                     Certificate[] chain,
                     PrivateKey pk, String disgestAlgorithm, String provider,
                     CryptoStandard subfilter,
                     String reason, String location, String imageUrl) throws IOException, DocumentException, BadElementException, GeneralSecurityException {
        //Creating the reader and the stamper 创建阅读器和压印器
        PdfReader reader = new PdfReader(src);
        FileOutputStream os = new FileOutputStream(dest);
        PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
        //Creating the appearance 创建外观
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setReason(reason);
        appearance.setLocation(location);
        appearance.setVisibleSignature(new Rectangle(400, 400, 200, 200), 1, "sig");
        Image image = Image.getInstance(imageUrl);
        appearance.setSignatureGraphic(image);
        appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
//        //获取页面内容
//        PdfContentByte contentByte = stamper.getOverContent(1);
//        //设置水印文字
//        Font watermarkFont = FontFactory.getFont(FontFactory.HELVETICA, 36, BaseFont.ITALICANGLE);
//        //绘制水印
//        contentByte.beginText();
//        contentByte.setFontAndSize(watermarkFont.getCalculatedSize(),watermarkFont.getSize());
//        contentByte.setColorGray(0.5f);
        //Creating the signature 创建签名
        BouncyCastleDigest digest = new BouncyCastleDigest();
        PrivateKeySignature signature = new PrivateKeySignature(pk, disgestAlgorithm, provider);
        MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, subfilter);
    }

    public static void sign(String SRC, String DEST) throws GeneralSecurityException, IOException, BadElementException, DocumentException {
        BouncyCastleProvider prvider = new BouncyCastleProvider();
        Security.addProvider(prvider);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(new FileInputStream(KEYSTORE), PASSWORD);
        String alias = (String) ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD);
        Certificate[] chain = ks.getCertificateChain(alias);
        C2_01_SignHelloWorld app = new C2_01_SignHelloWorld();
        app.sign(SRC, String.format(DEST, 1), chain, pk, DigestAlgorithms.SHA256, prvider.getName(), CryptoStandard.CMS, "Test 1", "Ghent", "D:\\pdf\\sign3.png");
    }
}
